//
//  CommonConfigHeaderView.m
//  GCDBenchmark
//
//  Created by Claude on 2025/10/13.
//

#import "CommonConfigHeaderView.h"

@interface CommonConfigHeaderView ()
// 折叠/展开相关
@property (nonatomic, assign, readwrite) BOOL isExpanded;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *toggleButton;
@property (nonatomic, strong) UILabel *summaryLabel;
@property (nonatomic, strong) UIView *configContentView;

// 原有控件
@property (nonatomic, strong) UILabel *taskCountLabel;
@property (nonatomic, strong) UILabel *taskCountValueLabel;
@property (nonatomic, strong) UIButton *taskCountMinusBtn;
@property (nonatomic, strong) UIButton *taskCountPlusBtn;
@property (nonatomic, strong) UISegmentedControl *taskCountStepSegment;
@property (nonatomic, strong) UILabel *taskDurationLabel;
@property (nonatomic, strong) UILabel *taskDurationValueLabel;
@property (nonatomic, strong) UIStepper *taskDurationStepper;
@property (nonatomic, strong) UILabel *taskTypeLabel;
@property (nonatomic, strong) UISegmentedControl *taskTypeSegment;

// 新增控件
@property (nonatomic, strong) UILabel *sleepSecondsLabel;
@property (nonatomic, strong) UILabel *sleepSecondsValueLabel;
@property (nonatomic, strong) UIStepper *sleepSecondsStepper;
@property (nonatomic, strong) UILabel *qosClassLabel;
@property (nonatomic, strong) UISegmentedControl *qosClassSegment;

// 状态
@property (nonatomic, assign) NSInteger currentTaskCount;
@property (nonatomic, assign) NSInteger currentTaskDuration;
@property (nonatomic, assign) NSInteger currentStepValue;
@property (nonatomic, assign) NSInteger currentSleepSeconds;
@property (nonatomic, assign) NSInteger currentQoSClass;

// 约束
@property (nonatomic, strong) NSLayoutConstraint *heightConstraint;
@end

@implementation CommonConfigHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.currentStepValue = 10; // 默认步进值，对应 segment index 1
        self.isExpanded = YES; // 默认展开
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor secondarySystemBackgroundColor];

    // 标题
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"📊 公共参数配置";
    self.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:self.titleLabel];

    // 折叠时显示的摘要
    self.summaryLabel = [[UILabel alloc] init];
    self.summaryLabel.font = [UIFont systemFontOfSize:15];
    self.summaryLabel.textColor = [UIColor labelColor];
    self.summaryLabel.numberOfLines = 0;
    self.summaryLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.summaryLabel.hidden = YES;
    [self addSubview:self.summaryLabel];

    // 折叠按钮（放在底部中间）
    self.toggleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.toggleButton setTitle:@"▲ 收起" forState:UIControlStateNormal];
    self.toggleButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    self.toggleButton.backgroundColor = [UIColor systemGray6Color];
    self.toggleButton.layer.cornerRadius = 8;
    [self.toggleButton addTarget:self action:@selector(toggleExpanded:) forControlEvents:UIControlEventTouchUpInside];
    self.toggleButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:self.toggleButton];

    // 配置内容容器
    self.configContentView = [[UIView alloc] init];
    self.configContentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:self.configContentView];

    // 1. 任务类型（第一行）
    self.taskTypeLabel = [[UILabel alloc] init];
    self.taskTypeLabel.text = @"任务类型:";
    self.taskTypeLabel.font = [UIFont systemFontOfSize:15];
    self.taskTypeLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.taskTypeLabel];

    self.taskTypeSegment = [[UISegmentedControl alloc] initWithItems:@[@"CPU密集", @"IO密集", @"CPU挂起"]];
    self.taskTypeSegment.selectedSegmentIndex = 0;
    [self.taskTypeSegment addTarget:self action:@selector(taskTypeChanged:) forControlEvents:UIControlEventValueChanged];
    self.taskTypeSegment.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.taskTypeSegment];

    // 2. 任务数量级选择器（第二行）
    self.taskCountStepSegment = [[UISegmentedControl alloc] initWithItems:@[@"1", @"10", @"100", @"1K", @"10K", @"100K"]];
    self.taskCountStepSegment.selectedSegmentIndex = 1;  // 默认选择 10
    [self.taskCountStepSegment addTarget:self action:@selector(stepValueChanged:) forControlEvents:UIControlEventValueChanged];
    self.taskCountStepSegment.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.taskCountStepSegment];

    // 3. 任务数量控制（第三行）
    self.taskCountLabel = [[UILabel alloc] init];
    self.taskCountLabel.text = @"任务数量:";
    self.taskCountLabel.font = [UIFont systemFontOfSize:15];
    self.taskCountLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.taskCountLabel];

    self.taskCountValueLabel = [[UILabel alloc] init];
    self.taskCountValueLabel.text = @"10";
    self.taskCountValueLabel.font = [UIFont monospacedDigitSystemFontOfSize:15 weight:UIFontWeightMedium];
    self.taskCountValueLabel.textAlignment = NSTextAlignmentCenter;
    self.taskCountValueLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.taskCountValueLabel];

    // 使用容器视图来包含加减按钮，模拟 UIStepper 样式
    UIView *stepperContainer = [[UIView alloc] init];
    stepperContainer.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:stepperContainer];

    // 减号按钮
    self.taskCountMinusBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.taskCountMinusBtn setTitle:@"−" forState:UIControlStateNormal];
    self.taskCountMinusBtn.titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightMedium];
    [self.taskCountMinusBtn addTarget:self action:@selector(taskCountMinus:) forControlEvents:UIControlEventTouchUpInside];
    self.taskCountMinusBtn.translatesAutoresizingMaskIntoConstraints = NO;
    [stepperContainer addSubview:self.taskCountMinusBtn];

    // 加号按钮
    self.taskCountPlusBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.taskCountPlusBtn setTitle:@"+" forState:UIControlStateNormal];
    self.taskCountPlusBtn.titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightMedium];
    [self.taskCountPlusBtn addTarget:self action:@selector(taskCountPlus:) forControlEvents:UIControlEventTouchUpInside];
    self.taskCountPlusBtn.translatesAutoresizingMaskIntoConstraints = NO;
    [stepperContainer addSubview:self.taskCountPlusBtn];

    // 设置容器和按钮的约束
    [NSLayoutConstraint activateConstraints:@[
        [stepperContainer.widthAnchor constraintEqualToConstant:94],
        [stepperContainer.heightAnchor constraintEqualToConstant:29],

        [self.taskCountMinusBtn.leadingAnchor constraintEqualToAnchor:stepperContainer.leadingAnchor],
        [self.taskCountMinusBtn.topAnchor constraintEqualToAnchor:stepperContainer.topAnchor],
        [self.taskCountMinusBtn.bottomAnchor constraintEqualToAnchor:stepperContainer.bottomAnchor],
        [self.taskCountMinusBtn.widthAnchor constraintEqualToConstant:47],

        [self.taskCountPlusBtn.trailingAnchor constraintEqualToAnchor:stepperContainer.trailingAnchor],
        [self.taskCountPlusBtn.topAnchor constraintEqualToAnchor:stepperContainer.topAnchor],
        [self.taskCountPlusBtn.bottomAnchor constraintEqualToAnchor:stepperContainer.bottomAnchor],
        [self.taskCountPlusBtn.widthAnchor constraintEqualToConstant:47],
    ]];

    // 4. 任务持续时间（第四行）
    self.taskDurationLabel = [[UILabel alloc] init];
    self.taskDurationLabel.text = @"任务时长(μs):";
    self.taskDurationLabel.font = [UIFont systemFontOfSize:15];
    self.taskDurationLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.taskDurationLabel];

    self.taskDurationValueLabel = [[UILabel alloc] init];
    self.taskDurationValueLabel.text = @"1000";
    self.taskDurationValueLabel.font = [UIFont monospacedDigitSystemFontOfSize:15 weight:UIFontWeightMedium];
    self.taskDurationValueLabel.textAlignment = NSTextAlignmentCenter;
    self.taskDurationValueLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.taskDurationValueLabel];

    self.taskDurationStepper = [[UIStepper alloc] init];
    self.taskDurationStepper.minimumValue = 100;
    self.taskDurationStepper.maximumValue = 100000;
    self.taskDurationStepper.stepValue = 100;
    self.taskDurationStepper.value = 1000;
    [self.taskDurationStepper addTarget:self action:@selector(taskDurationChanged:) forControlEvents:UIControlEventValueChanged];
    self.taskDurationStepper.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.taskDurationStepper];

    // 5. Sleep 秒数（第五行）
    self.sleepSecondsLabel = [[UILabel alloc] init];
    self.sleepSecondsLabel.text = @"Sleep(秒):";
    self.sleepSecondsLabel.font = [UIFont systemFontOfSize:15];
    self.sleepSecondsLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.sleepSecondsLabel];

    self.sleepSecondsValueLabel = [[UILabel alloc] init];
    self.sleepSecondsValueLabel.text = @"0";
    self.sleepSecondsValueLabel.font = [UIFont monospacedDigitSystemFontOfSize:15 weight:UIFontWeightMedium];
    self.sleepSecondsValueLabel.textAlignment = NSTextAlignmentCenter;
    self.sleepSecondsValueLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.sleepSecondsValueLabel];

    self.sleepSecondsStepper = [[UIStepper alloc] init];
    self.sleepSecondsStepper.minimumValue = 0;
    self.sleepSecondsStepper.maximumValue = 60;
    self.sleepSecondsStepper.stepValue = 1;
    self.sleepSecondsStepper.value = 0;
    [self.sleepSecondsStepper addTarget:self action:@selector(sleepSecondsChanged:) forControlEvents:UIControlEventValueChanged];
    self.sleepSecondsStepper.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.sleepSecondsStepper];

    // 6. QoS 类别（第六行）
    self.qosClassLabel = [[UILabel alloc] init];
    self.qosClassLabel.text = @"QoS:";
    self.qosClassLabel.font = [UIFont systemFontOfSize:15];
    self.qosClassLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.qosClassLabel];

    self.qosClassSegment = [[UISegmentedControl alloc] initWithItems:@[@"Interactive", @"Initiated", @"Default", @"Utility", @"Background", @"Unspecified"]];
    self.qosClassSegment.selectedSegmentIndex = 2;  // 默认选择 Default
    [self.qosClassSegment addTarget:self action:@selector(qosClassChanged:) forControlEvents:UIControlEventValueChanged];
    self.qosClassSegment.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configContentView addSubview:self.qosClassSegment];

    // 设置约束
    [NSLayoutConstraint activateConstraints:@[
        // 标题
        [self.titleLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:16],
        [self.titleLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:20],
        [self.titleLabel.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-20],

        // 摘要标签（折叠时显示）
        [self.summaryLabel.topAnchor constraintEqualToAnchor:self.titleLabel.bottomAnchor constant:12],
        [self.summaryLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:20],
        [self.summaryLabel.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-20],

        // 配置内容容器
        [self.configContentView.topAnchor constraintEqualToAnchor:self.titleLabel.bottomAnchor constant:16],
        [self.configContentView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
        [self.configContentView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],

        // 折叠按钮（放在底部中间）
        [self.toggleButton.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-12],
        [self.toggleButton.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
        [self.toggleButton.widthAnchor constraintEqualToConstant:100],
        [self.toggleButton.heightAnchor constraintEqualToConstant:32],
        [self.toggleButton.topAnchor constraintGreaterThanOrEqualToAnchor:self.configContentView.bottomAnchor constant:12],
        [self.toggleButton.topAnchor constraintGreaterThanOrEqualToAnchor:self.summaryLabel.bottomAnchor constant:12],
    ]];

    // 配置内容的约束
    [NSLayoutConstraint activateConstraints:@[
        // 1. 任务类型
        [self.taskTypeLabel.topAnchor constraintEqualToAnchor:self.configContentView.topAnchor],
        [self.taskTypeLabel.leadingAnchor constraintEqualToAnchor:self.configContentView.leadingAnchor constant:20],

        [self.taskTypeSegment.topAnchor constraintEqualToAnchor:self.taskTypeLabel.bottomAnchor constant:8],
        [self.taskTypeSegment.leadingAnchor constraintEqualToAnchor:self.configContentView.leadingAnchor constant:20],
        [self.taskTypeSegment.trailingAnchor constraintEqualToAnchor:self.configContentView.trailingAnchor constant:-20],

        // 2. 任务数量级选择器
        [self.taskCountStepSegment.topAnchor constraintEqualToAnchor:self.taskTypeSegment.bottomAnchor constant:16],
        [self.taskCountStepSegment.leadingAnchor constraintEqualToAnchor:self.configContentView.leadingAnchor constant:20],
        [self.taskCountStepSegment.trailingAnchor constraintEqualToAnchor:self.configContentView.trailingAnchor constant:-20],

        // 3. 任务数量控制
        [self.taskCountLabel.topAnchor constraintEqualToAnchor:self.taskCountStepSegment.bottomAnchor constant:16],
        [self.taskCountLabel.leadingAnchor constraintEqualToAnchor:self.configContentView.leadingAnchor constant:20],

        [stepperContainer.centerYAnchor constraintEqualToAnchor:self.taskCountLabel.centerYAnchor],
        [stepperContainer.trailingAnchor constraintEqualToAnchor:self.configContentView.trailingAnchor constant:-20],

        [self.taskCountValueLabel.centerYAnchor constraintEqualToAnchor:self.taskCountLabel.centerYAnchor],
        [self.taskCountValueLabel.trailingAnchor constraintEqualToAnchor:stepperContainer.leadingAnchor constant:-12],
        [self.taskCountValueLabel.widthAnchor constraintEqualToConstant:60],

        // 4. 任务持续时间
        [self.taskDurationLabel.topAnchor constraintEqualToAnchor:self.taskCountLabel.bottomAnchor constant:16],
        [self.taskDurationLabel.leadingAnchor constraintEqualToAnchor:self.configContentView.leadingAnchor constant:20],

        [self.taskDurationStepper.centerYAnchor constraintEqualToAnchor:self.taskDurationLabel.centerYAnchor],
        [self.taskDurationStepper.trailingAnchor constraintEqualToAnchor:self.configContentView.trailingAnchor constant:-20],

        [self.taskDurationValueLabel.centerYAnchor constraintEqualToAnchor:self.taskDurationLabel.centerYAnchor],
        [self.taskDurationValueLabel.trailingAnchor constraintEqualToAnchor:self.taskDurationStepper.leadingAnchor constant:-12],
        [self.taskDurationValueLabel.widthAnchor constraintEqualToConstant:60],

        // 5. Sleep 秒数
        [self.sleepSecondsLabel.topAnchor constraintEqualToAnchor:self.taskDurationLabel.bottomAnchor constant:16],
        [self.sleepSecondsLabel.leadingAnchor constraintEqualToAnchor:self.configContentView.leadingAnchor constant:20],

        [self.sleepSecondsStepper.centerYAnchor constraintEqualToAnchor:self.sleepSecondsLabel.centerYAnchor],
        [self.sleepSecondsStepper.trailingAnchor constraintEqualToAnchor:self.configContentView.trailingAnchor constant:-20],

        [self.sleepSecondsValueLabel.centerYAnchor constraintEqualToAnchor:self.sleepSecondsLabel.centerYAnchor],
        [self.sleepSecondsValueLabel.trailingAnchor constraintEqualToAnchor:self.sleepSecondsStepper.leadingAnchor constant:-12],
        [self.sleepSecondsValueLabel.widthAnchor constraintEqualToConstant:60],

        // 6. QoS 类别
        [self.qosClassLabel.topAnchor constraintEqualToAnchor:self.sleepSecondsLabel.bottomAnchor constant:16],
        [self.qosClassLabel.leadingAnchor constraintEqualToAnchor:self.configContentView.leadingAnchor constant:20],

        [self.qosClassSegment.topAnchor constraintEqualToAnchor:self.qosClassLabel.bottomAnchor constant:8],
        [self.qosClassSegment.leadingAnchor constraintEqualToAnchor:self.configContentView.leadingAnchor constant:20],
        [self.qosClassSegment.trailingAnchor constraintEqualToAnchor:self.configContentView.trailingAnchor constant:-20],
    ]];

    // 底部约束设置低优先级，避免折叠时冲突
    NSLayoutConstraint *bottomConstraint = [self.qosClassSegment.bottomAnchor constraintEqualToAnchor:self.configContentView.bottomAnchor];
    bottomConstraint.priority = UILayoutPriorityDefaultHigh;
    bottomConstraint.active = YES;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self toggleExpanded:self.toggleButton];
    });

}

- (void)updateWithTaskCount:(NSInteger)taskCount
                taskDuration:(NSInteger)taskDuration
                    taskType:(NSInteger)taskType
                sleepSeconds:(NSInteger)sleepSeconds
                    qosClass:(NSInteger)qosClass {
    // 启动时直接使用当前步进值作为任务数量
    self.currentTaskCount = self.currentStepValue;
    self.currentTaskDuration = taskDuration;
    self.currentSleepSeconds = sleepSeconds;
    self.currentQoSClass = qosClass;

    [self updateTaskCountDisplay];

    self.taskDurationValueLabel.text = [NSString stringWithFormat:@"%ld", (long)taskDuration];
    self.taskDurationStepper.value = taskDuration;

    self.taskTypeSegment.selectedSegmentIndex = taskType;

    self.sleepSecondsValueLabel.text = [NSString stringWithFormat:@"%ld", (long)sleepSeconds];
    self.sleepSecondsStepper.value = sleepSeconds;

    self.qosClassSegment.selectedSegmentIndex = [self qosClassToSegmentIndex:qosClass];

    [self updateSummaryLabel];

    // 通知delegate新的值
    if ([self.delegate respondsToSelector:@selector(taskCountDidChange:)]) {
        [self.delegate taskCountDidChange:self.currentStepValue];
    }
}

- (void)updateTaskCountDisplay {
    self.taskCountValueLabel.text = [NSString stringWithFormat:@"%ld", (long)self.currentTaskCount];
}

- (void)stepValueChanged:(UISegmentedControl *)segment {
    NSArray *stepValues = @[@1, @10, @100, @1000, @10000, @100000];
    self.currentStepValue = [stepValues[segment.selectedSegmentIndex] integerValue];

    // 切换数量级时，直接使用该数量级的值作为新的任务数量
    self.currentTaskCount = self.currentStepValue;
    [self updateTaskCountDisplay];

    if ([self.delegate respondsToSelector:@selector(taskCountDidChange:)]) {
        [self.delegate taskCountDidChange:self.currentStepValue];
    }

    [self updateSummaryLabel];
}

- (void)taskCountMinus:(UIButton *)button {
    NSInteger newValue = self.currentTaskCount - self.currentStepValue;
    // 最小值应该是当前步进值
    if (newValue < self.currentStepValue) {
        newValue = self.currentStepValue;
    }

    self.currentTaskCount = newValue;
    [self updateTaskCountDisplay];

    if ([self.delegate respondsToSelector:@selector(taskCountDidChange:)]) {
        [self.delegate taskCountDidChange:newValue];
    }

    [self updateSummaryLabel];
}

- (void)taskCountPlus:(UIButton *)button {
    NSInteger newValue = self.currentTaskCount + self.currentStepValue;
    if (newValue > 10000000) { // 最大值限制为1000万
        newValue = 10000000;
    }

    self.currentTaskCount = newValue;
    [self updateTaskCountDisplay];

    if ([self.delegate respondsToSelector:@selector(taskCountDidChange:)]) {
        [self.delegate taskCountDidChange:newValue];
    }

    [self updateSummaryLabel];
}

- (void)taskDurationChanged:(UIStepper *)stepper {
    NSInteger newValue = (NSInteger)stepper.value;
    self.currentTaskDuration = newValue;
    self.taskDurationValueLabel.text = [NSString stringWithFormat:@"%ld", (long)newValue];

    if ([self.delegate respondsToSelector:@selector(taskDurationDidChange:)]) {
        [self.delegate taskDurationDidChange:newValue];
    }

    [self updateSummaryLabel];
}

- (void)taskTypeChanged:(UISegmentedControl *)segment {
    NSInteger newType = segment.selectedSegmentIndex;

    if ([self.delegate respondsToSelector:@selector(taskTypeDidChange:)]) {
        [self.delegate taskTypeDidChange:newType];
    }

    [self updateSummaryLabel];
}

- (void)sleepSecondsChanged:(UIStepper *)stepper {
    NSInteger newValue = (NSInteger)stepper.value;
    self.currentSleepSeconds = newValue;
    self.sleepSecondsValueLabel.text = [NSString stringWithFormat:@"%ld", (long)newValue];

    if ([self.delegate respondsToSelector:@selector(sleepSecondsDidChange:)]) {
        [self.delegate sleepSecondsDidChange:newValue];
    }

    [self updateSummaryLabel];
}

- (void)qosClassChanged:(UISegmentedControl *)segment {
    NSInteger qosClass = [self segmentIndexToQosClass:segment.selectedSegmentIndex];
    self.currentQoSClass = qosClass;

    if ([self.delegate respondsToSelector:@selector(qosClassDidChange:)]) {
        [self.delegate qosClassDidChange:qosClass];
    }

    [self updateSummaryLabel];
}

- (void)toggleExpanded:(UIButton *)button {
    self.isExpanded = !self.isExpanded;

    [UIView animateWithDuration:0.3 animations:^{
        [button setTitle:self.isExpanded ? @"▲ 收起" : @"▼ 展开" forState:UIControlStateNormal];
        self.configContentView.hidden = !self.isExpanded;
        self.summaryLabel.hidden = self.isExpanded;

        // 更新高度
        CGFloat newHeight = self.isExpanded ? 380 : 120;
        CGRect frame = self.frame;
        frame.size.height = newHeight;
        self.frame = frame;

        // 如果是 TableView 的 header，需要重新设置
        if ([self.superview isKindOfClass:[UITableView class]]) {
            UITableView *tableView = (UITableView *)self.superview;
            tableView.tableHeaderView = self;
        }

        if (self.superview) {
            [self.superview layoutIfNeeded];
        }
    }];
}

- (void)updateSummaryLabel {
    NSString *taskTypeStr = @[@"CPU密集", @"IO密集", @"CPU挂起"][self.taskTypeSegment.selectedSegmentIndex];
    NSString *qosStr = @[@"Interactive", @"Initiated", @"Default", @"Utility", @"Background", @"Unspecified"][self.qosClassSegment.selectedSegmentIndex];

    NSString *summary = [NSString stringWithFormat:@"类型: %@  |  数量: %ld  |  时长: %ldμs\nSleep: %lds  |  QoS: %@",
                         taskTypeStr,
                         (long)self.currentTaskCount,
                         (long)self.currentTaskDuration,
                         (long)self.currentSleepSeconds,
                         qosStr];

    self.summaryLabel.text = summary;
}

- (NSInteger)segmentIndexToQosClass:(NSInteger)index {
    switch (index) {
        case 0: return QOS_CLASS_USER_INTERACTIVE;
        case 1: return QOS_CLASS_USER_INITIATED;
        case 2: return QOS_CLASS_DEFAULT;
        case 3: return QOS_CLASS_UTILITY;
        case 4: return QOS_CLASS_BACKGROUND;
        case 5: return QOS_CLASS_UNSPECIFIED;
        default: return QOS_CLASS_DEFAULT;
    }
}

- (NSInteger)qosClassToSegmentIndex:(NSInteger)qosClass {
    switch (qosClass) {
        case QOS_CLASS_USER_INTERACTIVE: return 0;
        case QOS_CLASS_USER_INITIATED: return 1;
        case QOS_CLASS_DEFAULT: return 2;
        case QOS_CLASS_UTILITY: return 3;
        case QOS_CLASS_BACKGROUND: return 4;
        case QOS_CLASS_UNSPECIFIED: return 5;
        default: return 2; // Default
    }
}

@end
